package com.cryptoneedle.garden.entity.garden.mapping;

import com.bubbles.engine.data.core.entity.BaseEntity;
import com.cryptoneedle.garden.enums.JoinType;
import com.cryptoneedle.garden.key.ColumnKey;
import com.cryptoneedle.garden.key.JoinKey;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.hibernate.annotations.Comment;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

/**
 * <p>description: Mapping-SourceColumn </p>
 *
 * @author CryptoNeedle
 * @date 2025-09-25
 */
@Getter
@Setter
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(schema = "public", name = "doris_column_mapping_join")
@IdClass(JoinKey.class)
@Comment("Doris-表映射Join信息")
public class MappingJoin extends BaseEntity {

    @Id
    @Column(length = 512)
    @Comment("主目录名称")
    private String primaryCatalogName;

    @Id
    @Column(length = 64)
    @Comment("主库名称")
    private String primaryDatabaseName;

    @Id
    @Column(length = 64)
    @Comment("主表名称")
    private String primaryTableName;

    @Id
    @Column(length = 64)
    @Comment("主表列名称")
    private String primaryColumnName;

    @Id
    @Enumerated(EnumType.STRING)
    @Comment("关联字段类型")
    private JoinType joinType;

    @Id
    @Column(length = 512)
    @Comment("关联目录名称")
    private String joinCatalogName;

    @Id
    @Column(length = 64)
    @Comment("关联库名称")
    private String joinDatabaseName;

    @Id
    @Column(length = 64)
    @Comment("关联表名称")
    private String joinTableName;

    @Id
    @Column(length = 64)
    @Comment("关联表列名称")
    private String joinColumnName;

    public ColumnKey primaryColumnKey() {
        return new ColumnKey(this.primaryCatalogName, this.primaryDatabaseName, this.primaryTableName, this.primaryColumnName);
    }

    public ColumnKey joinColumnKey() {
        return new ColumnKey(this.joinCatalogName, this.joinDatabaseName, this.joinTableName, this.joinColumnName);
    }

    public JoinKey joinKey() {
        return new JoinKey(this.primaryCatalogName, this.primaryDatabaseName, this.primaryTableName, this.primaryColumnName, this.joinType, this.joinCatalogName, this.joinDatabaseName, this.joinTableName, this.joinColumnName);
    }

    public String joinKeyString() {
        return joinKey().toString();
    }
}